﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MedicalManagementSystem.Utils;

namespace MedicalManagementSystem.Models
{
    public partial class DiseaseNote : IValidatable
    {
        public static DiseaseNote GetByPK(int id)
        {
            return DbContext.Instance.DiseaseNote.Where(diseaseNote => diseaseNote.id == id).FirstOrDefault();
        }

        public void Load()
        {
            if (EntityState != System.Data.EntityState.Added && EntityState != System.Data.EntityState.Detached)
            {
                if (!this.DiagnosReference.IsLoaded)
                {
                    this.DiagnosReference.Load();
                }
            }
        }

        public bool IsClosed
        {
            get { return DateEnd.HasValue; }
        }

        public int DiagnosId
        {
            get
            {
                return Diagnos == null ? 0 : Diagnos.id;
            }
            set { Diagnos = Diagnos.GetByPK(value); }
        }

        public bool CheckBeforeSave()
        {
            return GetRuleViolations().Count() == 0;
        }

        public IEnumerable<IRuleViolation> GetRuleViolations()
        {
            if (DateStart==DateTime.MinValue)
            {
                yield return new RuleViolation("Неверный формат даты.", this, "DateStart", DateStart);
            }
        }
    }
}
